import UA from 'utils/ua'
import env from 'utils/env'
import storage from 'utils/helper/storage'

// 非生产环境设置不为空生效
const TOKEN = '45z1EBg2jGeAxN6tb3wvrOBHJU7C0Rhm91CKVV+OJVvmhuvaf0Y54s3/0QKswMp3'

// 调试使用
const DEBUG = env === 'development' // debug开启的情况下，M站调试渠道默认为'jinjiang'，token为上面的token

// 小程序在不同公众号对应的channel code映射
const MP_CHANNEL_CODE_MAP = {
  wx5472cc88f668c6cd: 20003, // 锦江趣旅游公众号
  wx6e57898236fd4378: 20004, // 锦江会员俱乐部公众号
  wxe196afa36a2a4cf0: 20004 // 锦江会员俱乐部测试公众号
}

/**
 * 获取小程序的channel-code
 */
const getMpChannelCode = () => {
  // #ifdef MP-WEIXIN
  let launchOptions = wx.getLaunchOptionsSync()
  
  if (launchOptions.referrerInfo) {
    let sourceAppId = launchOptions.referrerInfo.appId

    return MP_CHANNEL_CODE_MAP[sourceAppId] || 20003
  }
  // #endif
  return 20003
}


// 字典来源 http://wiki.pp.plateno.cc/pages/viewpage.action?pageId=11207489
let map = {
  // 鉴权渠道 创新中心网关
  authType: {
    botao: 1,
    jinjiang: 3,
    bigshanghai: 10,
    weixin: 2,
    web: 5,
    // mdt: 6,
    mini: 4
  },

  uniType: {// 105-锦江app, 338-m站
    botao: 105,
    jinjiang: 105,
    bigshanghai: 338,
    weixin: 338,
    web: 338,
    mini: 338
  },

  cityChannelCode: {
    botao: 20001,
    jinjiang: 20001,
    bigshanghai: 20001,
    weixin: 20004,
    web: 20001,
    // mdt: 6,
    mini: getMpChannelCode()
  },

  // 下单渠道 同铂物馆
  channel: {
    botao: 105, // 铂涛app
    jinjiang: 168, // 锦江app
    weixin: 90044, // 默认铂涛旅行
    weixin_botao: 90044, // 铂涛旅行公众号
    weixin_jinjiang: 167, // 锦江酒店公众号
    weixin_prime: 349, // 锦江酒店尊享会公众号
    web: 338,
    bigshanghai: 90123,
    // mdt: 101, // 门店通
    mini: 313 // 小程序
  }
}

// 当前运行平台,区分微信 APP
const PLATFORM = (() => {
  let platform = 'jinjiang'

  // #ifdef MP
  platform = 'mini'
  // #endif

  // #ifdef H5
  if (UA.isWxWebview) {
    platform = 'weixin'
  } else if (UA.isBotaoWebview) {
    platform = 'botao'
  } else if (UA.isJinjiangWebview) {
    platform = 'jinjiang'
  } else if (UA.isBigShangHai) {
    platform = 'bigshanghai'
  } else {
    platform = 'web'

    // if (DEBUG) {
    //   platform = 'botao'
    // }
  }
  // #endif
  
  return platform
})()
// navigator.userAgent

const ENV = env

const getAuthType = () => {
  return map.authType[PLATFORM]
}

const getUniType = () => {  
  return map.uniType[PLATFORM]
}

const getCityChannelCode = () => {  
  return map.cityChannelCode[PLATFORM]
}

const getChannel = () => {
  let channel = storage.get('platform-channel')
  // 锦江酒店公众号167 尊享会员公众号349 从storage里拿
  if (PLATFORM == 'weixin' && channel) {
    return +channel
  }
  return map.channel[PLATFORM]
}
let CHANNEL = getChannel()
const updateChannel = () => {
  CHANNEL = getChannel()
}
const AUTH_TYPE = getAuthType()
const UNI_TYPE = getUniType()
const CITY_CHANNEL_CODE = getCityChannelCode()
export {
  DEBUG,
  ENV,
  UA,
  TOKEN,
  AUTH_TYPE,
  UNI_TYPE,
  CITY_CHANNEL_CODE,
  CHANNEL,
  PLATFORM,
  getAuthType,
  getChannel,
  updateChannel,
  map
}
